import { _decorator, Animation } from 'cc';
import { ENTITY_STATE_ENUM, PARAMS_NAME_ENUM } from '../../Enums';
import { getInitParamsNumber, getInitParamsTrigger, StateMachine } from '../../Base/StateMachine';
import IdleSubStateMachine from './IdleSubStateMachine';
import TrunSubStateMachine from './TurnLeftSubStateMachine';
import BlockFrontSubStateMachine from './BlockFrontSubStateMachine';
import { EntityManager } from '../../Base/EntityManager';
import BlockTurnLeftSubStateMachine from './BlockTurnLeftSubStateMachine';
import DeathSubStateManchine from './DeathSubStateManchine';
import AttackSubStateMachine from './AttackSubStateMachine';
import AirDeathSubStateManchine from './AirDeathSubStateManchine';
const { ccclass, property } = _decorator;


@ccclass('PlayerStateMachine')
export class PlayerStateMachine extends StateMachine {


    async init() {
        this.animationComponent = this.node.addComponent(Animation);
        this.initParams();
        this.initStateMachines();
        this.initAnimationEvent();

        await Promise.all(this.waitingList);
    }

    initParams() {
        this.params.set(PARAMS_NAME_ENUM.IDLE, getInitParamsTrigger());
        this.params.set(PARAMS_NAME_ENUM.TURNLEFT, getInitParamsTrigger());
        this.params.set(PARAMS_NAME_ENUM.BLOCKRONT, getInitParamsTrigger());
        this.params.set(PARAMS_NAME_ENUM.BLOCKTURNLEFT, getInitParamsTrigger());
        this.params.set(PARAMS_NAME_ENUM.DEATH, getInitParamsTrigger());
        this.params.set(PARAMS_NAME_ENUM.AIRDEATH, getInitParamsTrigger());
        this.params.set(PARAMS_NAME_ENUM.ATTACK, getInitParamsTrigger());
        this.params.set(PARAMS_NAME_ENUM.DIRECTION, getInitParamsNumber());
    }

    initStateMachines() {
        this.statMachines.set(PARAMS_NAME_ENUM.IDLE, new IdleSubStateMachine(this));
        this.statMachines.set(PARAMS_NAME_ENUM.TURNLEFT, new TrunSubStateMachine(this));
        this.statMachines.set(PARAMS_NAME_ENUM.ATTACK, new AttackSubStateMachine(this));
        this.statMachines.set(PARAMS_NAME_ENUM.DEATH, new DeathSubStateManchine(this));
        this.statMachines.set(PARAMS_NAME_ENUM.AIRDEATH, new AirDeathSubStateManchine(this));
        this.statMachines.set(PARAMS_NAME_ENUM.BLOCKRONT, new BlockFrontSubStateMachine(this));
        this.statMachines.set(PARAMS_NAME_ENUM.BLOCKTURNLEFT, new BlockTurnLeftSubStateMachine(this));

    }

    initAnimationEvent() {
        this.animationComponent.on(Animation.EventType.FINISHED, () => {
            const name = this.animationComponent.defaultClip.name;
            const whiteList = ['block', 'turn', 'attack'];
            if (whiteList.some(v => name.includes(v))) {
                this.node.getComponent(EntityManager).state = ENTITY_STATE_ENUM.IDLE;
            }
        })
    }

    run() {
        switch (this.currentState) {
            case this.statMachines.get(PARAMS_NAME_ENUM.TURNLEFT):
            case this.statMachines.get(PARAMS_NAME_ENUM.BLOCKRONT):
            case this.statMachines.get(PARAMS_NAME_ENUM.BLOCKTURNLEFT):
            case this.statMachines.get(PARAMS_NAME_ENUM.DEATH):
            case this.statMachines.get(PARAMS_NAME_ENUM.AIRDEATH):
            case this.statMachines.get(PARAMS_NAME_ENUM.IDLE):
            case this.statMachines.get(PARAMS_NAME_ENUM.ATTACK):
                if (this.params.get(PARAMS_NAME_ENUM.BLOCKRONT).value) {
                    this.currentState = this.statMachines.get(PARAMS_NAME_ENUM.BLOCKRONT);
                } else if (this.params.get(PARAMS_NAME_ENUM.ATTACK).value) {
                    this.currentState = this.statMachines.get(PARAMS_NAME_ENUM.ATTACK);
                } else if (this.params.get(PARAMS_NAME_ENUM.DEATH).value) {
                    this.currentState = this.statMachines.get(PARAMS_NAME_ENUM.DEATH);
                } else if (this.params.get(PARAMS_NAME_ENUM.AIRDEATH).value) {
                    this.currentState = this.statMachines.get(PARAMS_NAME_ENUM.AIRDEATH);
                } else if (this.params.get(PARAMS_NAME_ENUM.TURNLEFT).value) {
                    this.currentState = this.statMachines.get(PARAMS_NAME_ENUM.TURNLEFT);
                } else if (this.params.get(PARAMS_NAME_ENUM.BLOCKTURNLEFT).value) {
                    this.currentState = this.statMachines.get(PARAMS_NAME_ENUM.BLOCKTURNLEFT);
                } else if (this.params.get(PARAMS_NAME_ENUM.IDLE).value) {
                    this.currentState = this.statMachines.get(PARAMS_NAME_ENUM.IDLE);
                } else {
                    this.currentState = this.currentState;
                }
                break;
            default:
                this.currentState = this.statMachines.get(PARAMS_NAME_ENUM.IDLE);
        }
    }
}


